 /*
 * @name: 远程任务报名
 * @Author: along
 * @Date: 2019-06-15
 * @Last Modified by: along
 * @Last Modified time: 2020-06-15
 */
<template>
    <div class="container workSignUp">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">任务名称:</p>
                    <el-input
                        v-model="filterHead.task_name"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">发布公司:</p>
                    <el-input
                        v-model="filterHead.task_company"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">任务类型:</p>
                    <el-select
                        v-model="filterHead.task_type"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in taskList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"/>
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">上架状态:</p>
                    <el-select
                        v-model="filterHead.task_self"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in selfList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"/>
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">任务状态:</p>
                    <el-select
                        v-model="filterHead.task_status"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"/>
                    </el-select>
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="()=>{
                            this.$router.push({
                                name: 'workTaskEdit'
                            })
                        }"
                    >
                        添加任务
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="workSignUp"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="title"
                label="任务名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="hire_info.company_name"
                label="发布公司"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="category"
                label="任务类型"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.category == 1">文字</p>
                    <p v-if="scope.row.category == 2">图片</p>
                    <p v-if="scope.row.category == 3">问卷</p>
                    <p v-if="scope.row.category == 4">短视频</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="all_count"
                label="任务总数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable
                :width="110"
            />
            <el-table-column
                prop="verify_pass_count"
                label="已完成数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable
                :width="110"
            />
            <el-table-column
                prop="award_money"
                label="任务酬劳"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable
                :width="110"
            />
            <el-table-column
                prop="sort"
                label="排序"
                sortable
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="view_count"
                label="浏览量"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable
                :width="110"
            />
            <el-table-column
                prop="status"
                label="是否上架"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="fnIsShow(scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="task_status"
                label="任务状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.task_status == 3">已过期</p>
                    <p v-if="scope.row.task_status == 2">已完成</p>
                    <p v-if="scope.row.task_status == 1">进行中</p>
                    <p v-if="scope.row.task_status == 0">未开始</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="start_time"
                label="任务时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="140"
            >
                <template slot-scope="scope">
                    {{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }} -
                    {{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
                :width="170"
            >
                <template slot-scope="scope">
                    <div style="display:flex">
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="fnEditTask(scope.row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="fnCheckData(scope.row)"
                        >
                            查看提交
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="fnRemoveTask(scope.row)"
                        >
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            filterHead: {
                task_name: '',
                task_company: '',
                task_type: 99,
                task_self: 99,
                task_status: 99
            },
            statusList: [{
                value: 99,
                label: '全部'
            },{
                value: 0,
                label: '未开始'
            },{
                value: 1,
                label: '进行中'
            },{
                value: 3,
                label: '已过期'
            },{
                value: 2,
                label: '已完成'
            }],
            selfList: [{
                value: 99,
                label: '全部'
            },{
                value: 1,
                label: '已上架'
            },{
                value: 0,
                label: '已下架'
            }],
            taskList: [{
                value: 99,
                label: '全部'
            },{
                value: 1,
                label: '文字'
            },{
                value: 2,
                label: '图片'
            },{
                value: 3,
                label: '问卷'
            },{
                value: 4,
                label: '短视频'
            }],
            filterData: {
                sort_field: 0,
                sort_type: 1
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '工作'},{title: '远程工作报名'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/taskonline%5Cget_list', {
                page_id: page || this.page.now_page,
                title: this.filterHead.task_name,
                company_name: this.filterHead.task_company,
                category: this.filterHead.task_type,
                status: this.filterHead.task_self,
                task_status: this.filterHead.task_status,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 查看提交
         */
        fnCheckData (row) {
            this.$router.push({
                name: 'workTasktration',
                query: {
                    task_no: row.task_no
                }
            });
        },

        /**
         * 编辑任务
         */
        fnEditTask (row) {
            this.$router.push({
                name: 'workTaskEdit',
                query: {
                    task_no: row.task_no
                }
            });
        },

        /**
         * 排序
         */
        fnEditSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/student/taskonline%5Cset_sort',{
                    task_no: row.task_no,
                    sort: value
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '成功',
                            message: '操作成功',
                            type: 'success'
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '警告',
                            message: resp.msg,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 是否展示
         */
        fnIsShow (row) {
            this.$post('/student/taskonline%5Cset_status',{
                task_no: row.task_no,
                status: row.status
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '成功',
                        message: '操作成功',
                        type: 'success'
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '警告',
                        message: resp.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 删除任务
         */
        fnRemoveTask (row) {
            this.$confirm('是否要删除该条任务', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/taskonline%5Cdelete',{
                    task_no: row.task_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '成功',
                            message: '删除成功',
                            type: 'success'
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '警告',
                            message: resp.msg,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 数据排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 'asc',
                descending: 'desc'
            };

            let sortFieldMap = {
                all_count: 1,
                verify_pass_count: 2,
                award_money: 3,
                view_count: 4,
                sort: 5,
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * 查看图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning'
                });
            }
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .workSignUp{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
    }
</style>